package com.liming.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liming.model.Orders;
import com.liming.model.User;
import com.liming.service.OrdersService;

@WebServlet("/Orders")
public class OrdersServlet extends HttpServlet {

	OrdersService os = new OrdersService();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		
		String method = (String)request.getParameter("method");
		if(method.equals("createOrder")){
			createOrder(request,response);
		}
		else if (method.equals("cancelOrder")) {
			cancelOrder(request,response);
		}
		else if (method.equals("payOffOrder")) {
			payOffOrder(request,response);
		}
		else if (method.equals("getAllOrder")) {
			getAllOrder(request,response);
		}
		else if (method.equals("filterOrder")) {
			filterOrder(request,response);
		}
	}

	
	
	
	private void filterOrder(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		request.setCharacterEncoding("utf-8");
		
		User user = (User)request.getSession().getAttribute("user");
		int userid = user.getUserid();
		String keyword = (String)request.getParameter("keyword");
		
		List<Orders> orders= os.filterOrder(userid,keyword);
		
		request.setAttribute("FilterOrders", orders);
		//显示关键词订单 #.jsp
		request.getRequestDispatcher("#.jsp").forward(request, response);
		
	}




	private void getAllOrder(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		request.setCharacterEncoding("utf-8");
		
		User user = (User)request.getSession().getAttribute("user");
		int userid = user.getUserid();
		
		List<Orders> orders= os.getAllOrder(userid);
		
		request.setAttribute("Orders", orders);
		//显示订单 #.jsp
		request.getRequestDispatcher("#.jsp").forward(request, response);
		
	}



	//未实现！
	private void payOffOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		
		User user = (User)request.getSession().getAttribute("user");
		int userid = user.getUserid();
		String ordernum = (String)request.getParameter("ordernum");
		
		
		//#################
		int state = 1 ;
		os.payOffOrder(userid,ordernum,state);
	}

	private void cancelOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		
		User user = (User)request.getSession().getAttribute("user");
		int userid = user.getUserid();
		
		String ordernum = (String)request.getParameter("ordernum");
		
		os.cancelOrder(userid,ordernum);
	}

	//创建订单
	private void createOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		
		Date now=new Date();
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
		
		User user = (User)request.getSession().getAttribute("user");
		int userid = user.getUserid();
		int goodsid = Integer.parseInt(request.getParameter("goodsid"));
		int goodsnum = Integer.parseInt(request.getParameter("goodsnum"));
		String useraddress = (String)request.getParameter("useraddress");
		String ordertime = ft.format(now);
		String ordernum = UUID.randomUUID().toString().replaceAll("-", "");
		
		Orders orders= new Orders(userid,goodsid,goodsnum,useraddress,ordertime,ordernum,0);
		
		os.createOrder(orders);
		
		
	}
}
